<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增项目')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-project-add">
            <h4 class="form-header h4">项目</h4>
            <div class="form-group">
                <label class="col-sm-3 control-label">项目类型：</label>
                <div class="col-sm-8">
                    <select name="projectType" class="form-control m-b" th:with="type=${@dict.getType('lh_project_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">项目编号：</label>
                <div class="col-sm-8">
                    <input name="projectNo" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">项目名称：</label>
                <div class="col-sm-8">
                    <input name="projectName" class="form-control" type="text">
                </div>
            </div>
            <h4 class="form-header h4">软件/设备</h4>
            <div id="software"></div>
            <div class="form-group">
                <a class="btn btn-primary dim" type="button" id="addMachine"><i class="fa fa-plus"></i></a>
            </div>
        </form>
    </div>
    <script type="text/html" id="typeTpl">
        <div class="machine">
            <div class="form-group">
                <label class="col-sm-3 control-label">软件/设备类型：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <select name="machineList[{{index}}].machineType" class="form-control m-b" th:with="type=${@dict.getType('lh_machine_type')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
    <!--                    <span class="input-group-addon btn-danger deleteMachine"><i class="fa fa-minus"></i></span>-->
                        <a class="input-group-addon btn btn-danger dim deleteMachine" onclick="deleteMachine(this)" type="button"><i class="fa fa-minus"></i></a>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">软件/设备型号：</label>
                <div class="col-sm-8">
                    <input name="machineList[{{index}}].machineNo" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">软件/设备名称：</label>
                <div class="col-sm-8">
                    <input name="machineList[{{index}}].machineName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">软件/设备状态：</label>
                <div class="col-sm-8">
                    <select name="machineList[{{index}}].status" class="form-control m-b" th:with="type=${@dict.getType('lh_machine_open')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <hr/>
        </div>
    </script>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "lh/project";
        var index = 0;
        $("#form-project-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-project-add').serialize());
            }
        }

        layui.use('laytpl', function() {
            var laytpl  = layui.laytpl;
            $('#addMachine').click(function() {
                laytpl($("#typeTpl")[0].innerHTML).render(index, function(html){
                    $("#software").append(html);
                    index++;
                });
            });

        });

        function deleteMachine(obj) {
            $.modal.confirm("确定要删除当前设备吗？", function() {
                $(obj).parents(".machine").remove();
                index--;
                calcIndex();
            });
        }

        function calcIndex() {
            index = 0;
            $(".machine").each(function(i, element) {
                $(element).find("select").eq(0).attr("name", "machineList[" + index + "].machineType");
                $(element).find("input").eq(1).attr("name", "machineList[" + index + "].machineNo");
                $(element).find("input").eq(2).attr("name", "machineList[" + index + "].machineName");
                $(element).find("select").eq(1).attr("name", "machineList[" + index + "].status");
                index++;
            });
        }


    </script>
</body>
</html>